package com.mybatis.mapper.impl;

import com.mybatis.domain.Users;
import com.mybatis.mapper.RoleMapper;
import com.mybatis.mapper.UsersMapper;
import com.mybatis.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户操作接口
 */
public class UsersMapperImpl implements UsersMapper {

    SqlSession session;

    @Override
    public List<Users> list() {
        try {
            session = MybatisUtil.getSqlSession();    // 获取session
            return session.getMapper(UsersMapper.class).list();  // 返回结果集
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(session);
        }
        return null;
    }

    @Override
    public Users findInfo(Integer userId) {
        try {
            session = MybatisUtil.getSqlSession();    // 获取session
            return session.getMapper(UsersMapper.class).findInfo(userId);  // 返回结果集
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(session);
        }
        return null;
    }

    @Override
    public int add(Users users) {
        try {
            session = MybatisUtil.getSqlSession();    // 获取session
            return session.getMapper(UsersMapper.class).add(users);  // 返回结果集
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(session);
        }
        return 0;
    }

    @Override
    public int update(Users users) {
        try {
            session = MybatisUtil.getSqlSession();    // 获取session
            return session.getMapper(UsersMapper.class).update(users);  // 返回结果集
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(session);
        }
        return 0;
    }

    @Override
    public int delete(Integer userId) {
        try {
            session = MybatisUtil.getSqlSession();    // 获取session
            int count = session.getMapper(UsersMapper.class).delete(userId);  // 返回结果集
//            session.commit();
            return count;
        } catch (Exception e) {
            e.printStackTrace();
//            session.rollback();
        } finally {
            MybatisUtil.closeSession(session);
        }
        return 0;
    }


    @Override
    public Users login(String userName, String userPassword) {
        try {
            session = MybatisUtil.getSqlSession();    // 获取session
            return session.getMapper(UsersMapper.class).
                    login(userName, userPassword);  // 返回结果集
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(session);
        }
        return null;
    }

    @Override
    public List<Users> listLike(String userName) {
        try {
            session = MybatisUtil.getSqlSession();    // 获取session
            return session.getMapper(UsersMapper.class).listLike(userName);  // 返回结果集
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(session);
        }
        return null;
    }

    @Override
    public List<Users> listFenye(Integer from, Integer pageSize) {
        try {
            session = MybatisUtil.getSqlSession();    // 获取session
            return session.getMapper(UsersMapper.class).listFenye(from, pageSize);  // 返回结果集
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(session);
        }
        return null;
    }

    @Override
    public List<Users> list2(Users users) {
        try {
            session = MybatisUtil.getSqlSession();    // 获取session
            return session.getMapper(UsersMapper.class).list2(users);  // 返回结果集
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(session);
        }
        return null;
    }

    @Override
    public List<Users> listByIn(List<Integer> userIdList) {
        try {
            session = MybatisUtil.getSqlSession();    // 获取session
            return session.getMapper(UsersMapper.class).listByIn(userIdList);  // 返回结果集
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(session);
        }
        return null;
    }

    @Override
    public int deleteByIn(int[] userIdArray) {
        try {
            session = MybatisUtil.getSqlSession();    // 获取session
            return session.getMapper(UsersMapper.class).deleteByIn(userIdArray);  // 返回结果集
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(session);
        }
        return 0;
    }

    @Override
    public List<Users> listByUserAnaRole() {
        try{
            session = MybatisUtil.getSqlSession();    // 获取session
            return session.getMapper(UsersMapper.class).listByUserAnaRole();  // 返回结果集
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSession(session);
        }
        return null;
    }

    @Override
    public List<Users> listByUserAnaRole2() {
        try{
            session = MybatisUtil.getSqlSession();    // 获取session
            return session.getMapper(UsersMapper.class).listByUserAnaRole2();  // 返回结果集
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSession(session);
        }
        return null;
    }


    public static void main(String[] args) {
//        List<Users> list = new UsersMapperImpl().list();
//        for (Users item: list) {
//            System.out.println(item);
//        }
//        Users user = new UsersMapperImpl().findInfo(1);
//        System.out.println(user);


//        Users user2 = new Users(null, "taishanghuang",
//                "88888888",new Date(), 1);
//        int count = new UsersMapperImpl().add(user2);
//        if(count>0){
//            System.out.println("成功");
//        }


//        Users user3 = new Users(4, "laojun",
//                "9999",new Date(), 2);
//        int count3 = new UsersMapperImpl().update(user3);
//        if(count3>0){
//            System.out.println("成功");
//        }
//        new UsersMapperImpl().delete(4);


//        System.out.println(new UsersMapperImpl().login("root","666"));
//        List<Users> list = new UsersMapperImpl().listLike("2");
//        for (Users item: list) {
//            System.out.println(item);
//        }

//        int dangqianye = 5;  //当前页
//        int pageSize = 2; // 每页显示的记录数
//
//        List<Users> list = new UsersMapperImpl().
//                listFenye((dangqianye-1)*pageSize,pageSize);
//        for (Users item: list) {
//            System.out.println(item);
//        }

//        Users user = new Users();
//        user.setUserName("a");
//        user.setUserPassword("9");
//        List<Users> list = new UsersMapperImpl().list2(user);
//        for (Users item : list) {
//            System.out.println(item);
//        }

//        List<Integer> userIdList = new ArrayList<>();
//        userIdList.add(1);
//        userIdList.add(3);
//        userIdList.add(5);
//
//        List<Users> list = new UsersMapperImpl().listByIn(userIdList);
//        for (Users item : list) {
//            System.out.println(item);
//        }

//        int[] userids = {5,3};
//        new UsersMapperImpl().deleteByIn(userids);


        List<Users> list = new UsersMapperImpl().listByUserAnaRole2();
        for (Users item : list) {
            System.out.println(item);
        }
    }
}
